package net.shopxx.dao.impl;

import net.shopxx.bean.Pager;
import net.shopxx.dao.ProductCommentDao;
import net.shopxx.entity.Member;
import net.shopxx.entity.Product;
import net.shopxx.entity.ProductComment;
import net.shopxx.entity.ProductConsult;

import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

/**
 * Dao实现类 - 订单项
 */

@Repository
public class ProductCommentDaoImpl extends BaseDaoImpl<ProductComment, String> implements ProductCommentDao {

	@Override
	public Pager getMemberCommentPager(Member member, Pager pager) {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ProductComment.class);
		detachedCriteria.createAlias("member", "member");
		detachedCriteria.add(Restrictions.eq("member.id", member.getId()));
		return findByPager(pager, detachedCriteria);
	}

	@Override
	public Pager getProductCommentPager(Product product, Pager pager) {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ProductComment.class);
		detachedCriteria.createAlias("product", "product");
		detachedCriteria.add(Restrictions.eq("product.id", product.getId()));
		return findByPager(pager, detachedCriteria);
	}

	@Override
	public Pager getMemberOwnCommentPager(Member member, Pager pager) {

		if(pager==null)
		{
			pager = new Pager();
		}
		Integer pageNumber = pager.getPageNumber();
		Integer pageSize = pager.getPageSize();
		String hql = "select pc from ProductComment as pc inner join pc.product as p with p.member.id=?";
		Query query = getSession().createQuery(hql).setParameter(0, member.getId());
		pager.setTotalCount(query.list().size());
		query.setFirstResult((pageNumber - 1) * pageSize);
		query.setMaxResults(pageSize);
		pager.setList(query.list());
		return pager;
	}
	
}